<?php

namespace App\Admin\Controllers;

use App\Admin\Export\UserMoneyLogExport;
use App\Admin\Tools\UserSettlement;
use App\Repositories\UserRepository;
use App\User;
use App\UserMoneyLog;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;

class UserMoneyLogController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('会员结算明细');
            $content->description('会员结算明细');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(UserMoneyLog::class, function (Grid $grid) {
            $grid->model()
                ->join('users','users.id' ,'=','user_money_log.leader_id')
                ->join('order_pay','order_pay.order_pay_id','=','user_money_log.order_pay_id')->select('users.mobile','users.name','user_money_log.type','user_money_log.status','user_money_log.price','order_pay.pay_price','order_pay.total_money','user_money_log.created_at','user_money_log.updated_at','order_pay.pay_sn','user_money_log.order_pay_id','user_money_log.id','user_money_log.refund_status','order_pay.paid_at');
            $grid->name('受益人昵称');
            $grid->mobile('手机号');
            $grid->price('收益金额')->display(function($value){

                if($this->refund_status==UserMoneyLog::REFUND_STATUS_APPLIED){
                    return '-'.$value;
                }else{
                    return '+'.$value;
                }
            });
            $grid->type('类型')->display(function($value){
                return $value ? '团队收益' : '推广收益';
            });
            $grid->status('是否结算')->display(function($value){
                return $value ? '已结算' : '未结算';
            });
            $grid->paid_at('生成时间');
            $grid->updated_at('结算时间')->display(function($value){
                return $this->status ? $value : '未结算';
            });
            $grid->filter(function($filter){
                $filter->disableIdFilter();
                $filter->like('mobile','手机号');
                $filter->like('name','受益人昵称');
                $filter->equal('type','类型')->select(['推广收益','团队收益']);
                $filter->equal('status','状态')->select(['未结算','已结算']);

                $filter->between('paid_at', '销售时间')->datetime();
            });
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->exporter(new  UserMoneyLogExport());
            $grid->pay_sn('订单流水号')->display(function($value){
                return "<a href='/admin/order/".$this->order_pay_id."' target='_blank' > $value </a>";
            });
            //批量删除
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                    $batch->add('结算收益', new UserSettlement(1));

                });
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(UserMoneyLog::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
    public function settlement(Request $request){
      $ids=$request->ids;
      $userRespository=new UserRepository();
      $userRespository->setellement($ids);
        return \Response::json(['success']);
    }
}
